#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.sympy import *

def dot_prod(u, v):
    return sum(ui * vi for (ui, vi) in zip(u, v))

# Entradas
with display_block('Entradas'):
    func = x ** 2 + x * y + y ** 2
    ponto2d = (randint(-4, 4) / Two, randint(-4, 4) / Two)
    ponto = ponto2d + (func.subs(list(zip([x, y], ponto2d))),)
    plano = y - x + ponto[0] - ponto[1]

# Resultados auxiliares
with display_block('Variáveis auxiliares'):
    var_subs = list(zip([x, y, z], ponto))
    grad = list((func - z).diff(x) for x in [x, y, z])
    grad = list(df_i.subs(var_subs) for df_i in grad)
    plane_eq = dot_prod(grad, tuple(xi - pi for (xi, pi) in zip([x, y, z], ponto)))
    plane_x_eq_y = plane_eq.subs([(y, x)])
    plane_z0 = plane_eq.subs([(z, 0)])
    plane_ok = plane_z0.subs([(y, x)])

# Resultados
with display_block('Respostas'):
    _x = solve(plane_ok, x)[0]
    _y = solve(plano.subs(x, _x), y)[0]
    correta = (_x, _y)
    assert correta != (0, 0)
    x_eq_1 = (1, solve(plane_z0.subs([(x, 1)]), y)[0])
    x_eq_0 = (0, solve(plane_z0.subs([(x, 0)]), y)[0])
    y_eq_0 = (solve(plane_z0.subs([(y, 0)]), x)[0], 0)
    z_eq_1 = (solve(plane_x_eq_y.subs([(z, 1)]), x)[0],) * 2

